package kgame5.pbe.animation {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public final class AnimationBuilder
	{
		public static var isShowBG:Boolean = false;
		public static function importStrip(fps:int, bitmapData:BitmapData, tileWidth:int, tileHeight:int, tileCount:int=-1, startingX:int=0, startingY:int=0, rotations:int=1, offsetX:int=0, offsetY:int=0):Animation
		{
			var animation:Animation=new Animation(fps, rotations);
			var rows:int=Math.ceil(bitmapData.height / tileHeight);
			var cols:int=Math.ceil(bitmapData.width / tileWidth);
			var frameLocation:Rectangle;
			if (tileCount == -1)
			{
				tileCount=rows * cols;
			}
			for (var i:int=0; i < tileCount; i++)
			{
				var tileX:int=(i % cols);
				var tileY:int=Math.floor(i / cols);
				frameLocation=new Rectangle(startingX + tileX * tileWidth, startingY + tileY * tileHeight, tileWidth, tileHeight);
				animation.addFrame(1, bitmapData, frameLocation, offsetX, offsetY);
			}
			return animation;
		}

		/**
		 * Imports a MovieClip, Sprite, or Bitmap object and converts to an Animation object.
		 * @param a_displayObject The object to import. Must be a MovieClip, Sprite, or Bitmap.
		 * @param fps The desired frame rate of the Animation
		 * @param rotations The number of Precached rotations to create
		 * @param offsetX X pixels to offset by
		 * @param offsetY Y pixels to offset by
		 * @param a_startFrame If a MovieClip is being imported, this is the frame to start importing from
		 * @param a_endFrame If a MovieClip is being imported, this is the last frame to import
		 * @return The newly generated Animation object
		 *
		 */
		public static function importDisplayObject(displayObject:DisplayObject, fps:int, rotations:int=1, offsetX:Number=0, offsetY:Number=0, startFrame:int=1, endFrame:int=-1):Animation
		{
			var animation:Animation=new Animation(fps, rotations);
			if (displayObject is MovieClip)
			{
				return importMovieClip(displayObject as MovieClip, animation, offsetX, offsetY, startFrame, endFrame);
			}
			else if (displayObject is Sprite)
			{
				return importSprite(displayObject as Sprite, animation, offsetX, offsetY);
			}
			else if (displayObject is Bitmap)
			{
				return importBitmap(displayObject as Bitmap, animation, offsetX, offsetY);
			}
			return null;
		}
		
		/**
		 * Imports a MovieClip, Sprite, or Bitmap object and converts to an Animation object.
		 * @param a_displayObject The object to import. Must be a MovieClip, Sprite, or Bitmap.
		 * @param fps The desired frame rate of the Animation
		 * @param rotations The number of Precached rotations to create
		 * @param offsetX X pixels to offset by
		 * @param offsetY Y pixels to offset by
		 * @param a_startFrame If a MovieClip is being imported, this is the frame to start importing from
		 * @param a_endFrame If a MovieClip is being imported, this is the last frame to import
		 * @return The newly generated Animation object
		 *
		 */
		public static function importDisplayObject2(displayClass:Class, fps:int, rotations:int=1, offsetX:Number=0, offsetY:Number=0, startFrame:int=1, endFrame:int=-1):Animation
		{
			var displayObject:DisplayObject = new displayClass();
			var animation:Animation=new Animation(fps, rotations);
			if (displayObject is MovieClip)
			{
				return importMovieClip(displayObject as MovieClip, animation, offsetX, offsetY, startFrame, endFrame);
			}
			else if (displayObject is Sprite)
			{
				return importSprite(displayObject as Sprite, animation, offsetX, offsetY);
			}
			else if (displayObject is Bitmap)
			{
				return importBitmap(displayObject as Bitmap, animation, offsetX, offsetY);
			}
			return null;
		}

		private static function importMovieClip(movieClip:MovieClip, animation:Animation, offsetX:Number=0, offsetY:Number=0, startFrame:int=1, endFrame:int=-1):Animation
		{
			//--解决鱼抖动的问题。在鱼的mc中添加透明背景。
			var g:Graphics=movieClip.graphics;
			
			if(isShowBG)
			{
				g.beginFill(0xff0000,0.5);
			}
			else
			{
				g.beginFill(0xff0000,0);
			}
			
			// 画出原始大小的边框，不再进行二次缩放
			g.drawRect(0,0,movieClip.width/movieClip.scaleX,movieClip.height/movieClip.scaleY);
			g.endFill();
			//--
			var bd:BitmapData;
			var bounds:Rectangle;
			movieClip.gotoAndStop(startFrame);
			var ef:int=endFrame;
			if (ef == -1 || ef > movieClip.totalFrames)
			{
				ef=movieClip.totalFrames;
			}
			var i:int=startFrame;
			while (i <= ef)
			{
				bounds=movieClip.getBounds(movieClip);
				if(bd)bd.dispose();
				
				var m:Matrix = new Matrix(1, 0, 0, 1, -bounds.x, -bounds.y);
				m.scale(movieClip.scaleX, movieClip.scaleY);
				//bd=new BitmapData(Math.max(1, movieClip.width/* Math.min(2880, bounds.width) */), Math.max(1, movieClip.height/*  Math.min(2880, bounds.height) */), true, 0x00000000);
				//max修改，解决鱼被切的问题，原因是bmd.widht是int,而displayObject.width是Number
				var bmdWidth:int=Math.min(2800, Math.max(1, Math.ceil(movieClip.width)));
				var bmdHeight:int=Math.min(2800, Math.max(1, Math.ceil(movieClip.height)));
				
				bd=new BitmapData(bmdWidth,bmdHeight, true, 0x00000000);
				
				bd.draw(movieClip, m, null, null, null, true);
				animation.addFrame(1, bd, bd.rect, bounds.x + offsetX, bounds.y + offsetY, movieClip.currentLabel);
				movieClip.nextFrame();
				i++;
			}
			if(bd)bd.dispose();
			return animation;
		}

		private static function importSprite(sprite:Sprite, animation:Animation, offsetX:Number=0, offsetY:Number=0):Animation
		{
			var bounds:Rectangle=sprite.getBounds(sprite);
			var bd:BitmapData=new BitmapData(Math.max(1, Math.min(2880, bounds.width)), Math.max(1, Math.min(2880, bounds.height)), true, 0x00000000);
			bd.draw(sprite, new Matrix(1, 0, 0, 1, -bounds.x, -bounds.y), null, null, null, true);
			animation.addFrame(1, bd, bd.rect, bounds.x + offsetX, bounds.y + offsetY);
			bd.dispose();
			return animation;
		}

		private static function importBitmap(bitmap:Bitmap, animation:Animation, offsetX:Number=0, offsetY:Number=0):Animation
		{
			var bd:BitmapData=new BitmapData(bitmap.width, bitmap.height, true, 0x00000000);
			bd.copyPixels(bitmap.bitmapData, bitmap.bitmapData.rect, new Point());
			animation.addFrame(1, bd, bd.rect, offsetX, offsetY);
			bd.dispose();
			return animation;
		}
	}
}